package com.geostar.constructionland.analyse.service.automatic.util;

import java.io.*;

public class FileUtil {

    /**
     * 创建文件
     * @param filePath
     * @param fileContent
     * @return
     */
    public static boolean createFile(String filePath, String fileContent) {
        Boolean result = false;
        //将写入转化为流的形式
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(filePath));
            bw.write(fileContent);
            result = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 删除文件
     * @param filePath
     * @return
     */
    public  static boolean delFile(String filePath) {
        Boolean result = false;
        File file = new File(filePath);
        try {
            if (file.exists()) {
                file.delete();
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 压缩包转字符串
     * @param filePath
     * @return
     */
    public static String getString(String filePath) {
        StringBuffer result = new StringBuffer();
        File file = new File(filePath);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        InputStreamReader iReader = null;
        BufferedReader reader = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            iReader = new InputStreamReader(bis, "GBK");
            reader = new BufferedReader(iReader);
            while (reader.ready()) {
                result.append((char)reader.read());
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (iReader != null) {
                    iReader.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result.toString();
    }
}
